package cn.play.cardgame.widget;

import java.util.Random;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.play.cardgame.R;
import cn.play.cardgame.data.FaPai;
import cn.play.cardgame.util.ScreenUtils;

public class ResultPanel extends FrameLayout {
	private Context mContext;
	@InjectView(R.id.result_panel_lottery)
	ImageView mResultLottery;
	@InjectView(R.id.result_panel_lottery_bg)
	ImageView mResultLotteryBg;
	private int width;

	public ResultPanel(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		initViews();
	}

	public ResultPanel(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ResultPanel(Context context) {
		this(context, null);
	}

	private void initViews() {
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.WRAP_CONTENT,
				FrameLayout.LayoutParams.MATCH_PARENT);

		View view = View.inflate(mContext, R.layout.result_panel, null);
		ButterKnife.inject(this, view);

		this.addView(view, params);
	}

	public void resetResultPanel(int height) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		// 设置为true,表示解析Bitmap对象，该对象不占内存
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.result_poker, options);

		int w = options.outWidth;// image.getWidth();
		int h = options.outHeight;// image.getHeight();
		width = (int) (((float) w / (float) h) * height);
		FrameLayout.LayoutParams param = new FrameLayout.LayoutParams(width,
				height);
		mResultLotteryBg.setLayoutParams(param);

		param = new FrameLayout.LayoutParams(width - ScreenUtils.dp2px(22), height - ScreenUtils.dp2px(22));
		param.gravity = Gravity.CENTER;
		mResultLottery.setLayoutParams(param);
	}

//	public int getResultWidth() {
//		return width;
//	}

	public void setLottery(String value, String huase) {
		mResultLottery.setBackgroundResource(FaPai.getImage(mContext, value,
				huase, 1));
		mResultLotteryBg.setVisibility(View.VISIBLE);
	}

	public void setRandomBeatuBackground() {
		Random random = new Random(System.currentTimeMillis());
		int endfix = random.nextInt(17) + 1; // 17张美女图
		int resId = mContext.getResources().getIdentifier(
				"result_poker_back_" + endfix, "drawable",
				mContext.getPackageName());
		mResultLottery.setBackgroundResource(resId);
		mResultLotteryBg.setVisibility(View.VISIBLE);
	}

	public void setDefaultBackground() {
		mResultLottery.setBackgroundResource(R.drawable.result_bg);
		mResultLotteryBg.setVisibility(View.INVISIBLE);
	}
}
